//
//  KKQuestionSortDialog.m
//  DoMi
//
//  Created by 康凯 on 2016/12/5.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKQuestionSortDialog.h"
#import "KKStudyItem.h"
#import "KKQuestionItemCell.h"


@interface KKQuestionSortDialog()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)NSMutableArray *items;
@property(copy,nonatomic)NSString *type;
@property(nonatomic,strong)UIButton *currentRadio;
@end

@implementation KKQuestionSortDialog

-(void)awakeFromNib{
    [super awakeFromNib];
    [self initData];
}

-(void)initData{
    self.frame = [UIScreen mainScreen].bounds;
    self.containerBottom.constant=-180;
    [self layoutIfNeeded];
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    //初始化数据
    NSArray *titles=@[@"热度由高到低",@"热度由低到高",@"奖金由高到低",@"奖金由低到高"];
    NSArray *codes=@[@"browser_up",@"browser_down",@"money_up",@"money_down"];
    self.items=[NSMutableArray array];
    for (int i=0;i<titles.count;i++) {
        KKStudyItem *item=[[KKStudyItem alloc] init];
        item.title=titles[i];
        item.code=codes[i];
        [self.items addObject:item];
    }
    self.type=@"browser_up";
    
    //确定是水平滚动，还是垂直滚动
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.itemSize=CGSizeMake((screenWidth-20)/2, 35);
    flowLayout.minimumLineSpacing=1;
    flowLayout.minimumInteritemSpacing=1;
    
    self.collectionView.collectionViewLayout=flowLayout;
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    self.collectionView.backgroundColor=[UIColor colorWithHexString:@"#E3E3E0"];
    self.collectionView.layer.cornerRadius=4;
    
    //注册Cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"KKQuestionItemCell" bundle:nil] forCellWithReuseIdentifier:@"KKQuestionItemCell"];
}

#pragma mark collectionview代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.items.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KKQuestionItemCell *cell = (KKQuestionItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"KKQuestionItemCell" forIndexPath:indexPath];
    KKStudyItem *item=self.items[indexPath.row];
    [cell initData:&item];
    if(indexPath.row==0){
        item.radio.selected=YES;
        self.currentRadio=item.radio;
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((screenWidth-15)/2, 35);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if(section==0){
        return UIEdgeInsetsMake(2, 2, 0, 2);
    }
    return UIEdgeInsetsMake(0, 2, 0, 2);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    KKStudyItem *item= self.items[indexPath.row];
    self.currentRadio.selected=NO;
    item.radio.selected=YES;
    self.currentRadio=item.radio;
    self.type=item.code;
}

#pragma makr 弹框--展示
-(void)show{
    [appWindow addSubview:self];
    [UIView animateWithDuration:dialog_animation_time animations:^{
        self.containerBottom.constant=0;
        [self layoutIfNeeded];
    }];
}
#pragma makr 弹框--消失
-(void)dismiss{
    [UIView animateWithDuration:dialog_animation_time animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        self.containerBottom.constant=-180;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)ok:(id)sender {
    [self dismiss];
    self.itemClickBlock(self.type);
}
#pragma mark 按钮--取消
- (IBAction)cancle:(id)sender {
    [self dismiss];
}

@end
